package edu.whu.powersoap;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import java.io.File;

public class UserProvider extends ContentProvider {

	private static final String TAG = "PoiProvider";

	public static final String AUTHORITY = "powersoap.user";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/");

	public static final String VOL_ID = "id";
	public static final String VOL_NAME = "name";
	public static final String VOL_PASSWORD = "password";
	public static final String VOL_FLAG = "flag";

	private static final String DATABASE_NAME = "user.db";
	private static final int DATABASE_VERSION = 1;

	private static final String TABLE_NAME = "user";

	private DatabaseHelper mOpenHelper;

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext(), DATABASE_NAME, null,
				DATABASE_VERSION);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();

		Cursor cursor = db.query(TABLE_NAME, projection, selection,
				selectionArgs, null, null, sortOrder);

		return cursor;
	}

	@Override
	public String getType(Uri uri) {

		Log.i(TAG, "getType---uri=" + uri);
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		SQLiteDatabase db = mOpenHelper.getWritableDatabase();

		/* now insert into database */
		long rowId = db.insert(TABLE_NAME, null, values);
		if (rowId > 0) {
			uri = ContentUris.withAppendedId(uri, rowId);
			sendNotify(uri);
			return uri;
		}
		throw new SQLException("Failed to insert row into: " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.delete(TABLE_NAME, selection, selectionArgs);
		if (count > 0)
			sendNotify(uri);

		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count = db.update(TABLE_NAME, values, selection, selectionArgs);

		sendNotify(uri);

		return count;
	}

	private void sendNotify(Uri uri) {
		getContext().getContentResolver().notifyChange(uri, null);

	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE IF NOT EXISTS "
					+ TABLE_NAME
					+ "(id INTEGER PRIMARY KEY, name TEXT, password TEXT, flag LONG)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrade database from " + oldVersion + " to "
					+ newVersion);

			Log.w(TAG, "drop all data!");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);

		}

	}

	public static UserInfo getUserFromName(Context context, String username) {
		UserInfo user = null;

		Cursor cursor = context.getContentResolver().query(
				CONTENT_URI,
				new String[] { VOL_ID + " as _id", VOL_NAME, VOL_PASSWORD,
						VOL_FLAG }, "NAME=?", new String[] { username }, null);

		if (cursor != null) {
			if (cursor.moveToFirst()) {
				user = new UserInfo();

				user.setName(cursor.getString(cursor.getColumnIndex(VOL_NAME)));
				user.setPassword(cursor.getString(cursor
						.getColumnIndex(VOL_PASSWORD)));
				user.setFlag(cursor.getLong(cursor.getColumnIndex(VOL_FLAG)));

			}
			cursor.close();
		}

		return user;

	}

	public static void addUser(Context context, String username,
			String password, long flag) {

		ContentResolver cr = context.getContentResolver();

		/* insert new */
		ContentValues values = new ContentValues();

		values.put(VOL_NAME, username);
		values.put(VOL_PASSWORD, password);
		values.put(VOL_FLAG, flag);

		cr.insert(CONTENT_URI, values);

		return;

	}

	public static void deleteUser(Context context, String username) {

		ContentResolver cr = context.getContentResolver();
		cr.delete(CONTENT_URI, "name=?", new String[] { username });

		return;

	}

	public static void changePassword(Context context, String username,
			String password) {

		ContentValues values = new ContentValues();
		values.put(VOL_PASSWORD, password);

		ContentResolver cr = context.getContentResolver();
		int count = cr.update(CONTENT_URI, values, "name=?",
				new String[] { username });

		return;

	}
	public static void changeFlag(Context context, String username, long flag){
		ContentValues values = new ContentValues();
		values.put(VOL_FLAG, flag);
		
		ContentResolver cr = context.getContentResolver();
		int count = cr.update(CONTENT_URI, values, "name=?", new String[] { username});
		return;
	}

}
